//
//  orderProductView.m
//  代理端
//
//  Created by mac on 16/3/8.
//  Copyright © 2016年 sliver. All rights reserved.
//

#import "orderProductView.h"
@interface orderProductView()

@property (weak, nonatomic) IBOutlet UILabel *reasonLable;
@property (weak, nonatomic) IBOutlet UIImageView *iconImage;
@property (weak, nonatomic) IBOutlet UILabel *productNameLable;
@property (weak, nonatomic) IBOutlet UILabel *productTypeLable;
@property (weak, nonatomic) IBOutlet UILabel *priceLable;
@property (weak, nonatomic) IBOutlet UILabel *numberLable;
@property (weak, nonatomic) IBOutlet UILabel *moneyLable;

@property (weak, nonatomic) IBOutlet UIButton *oneButten;

@property (weak, nonatomic) IBOutlet UIButton *twoButten;

@property(nonatomic,strong)OrderDetailModel *model;
@end
@implementation orderProductView

-(void)awakeFromNib
{
    self.oneButten.layer.masksToBounds = YES;
    self.oneButten.layer.cornerRadius = 5;
    
    self.twoButten.layer.masksToBounds = YES;
    self.twoButten.layer.cornerRadius =5;
    
    self.iconImage.layer.masksToBounds = YES;
    self.iconImage.layer.cornerRadius = 5;
    
    [AutoLayoutUtil autoLayoutWith:self.subviews];

}
-(void)drawRect:(CGRect)rect
{
    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:CGPointMake(10*KScr, CGRectGetMaxY(self.numberLable.frame)+5*KScr)];

    [path addLineToPoint:CGPointMake(ScreenW-20*KScr,  CGRectGetMaxY(self.numberLable.frame)+5*KScr)];
    
    [DRINK_LIGHTGRAY_COLOR set];
    
    [path stroke];
}
-(void)gaveMeMode:(OrderDetailModel*)model Withindex:(NSInteger)inter
{
    self.model = model;
    
//    model.tag = inter;
    //    NSString *url = [NSString stringWithFormat:@"%@%@",BASE_URL,model.img_thum_cover];
    
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@",BASE_URL,model.img_thum_cover]];
    
    [self.iconImage setImageWithURL:url placeholderImage:[UIImage imageNamed:@"代理商信息头像_150.png"]];
    
    self.productNameLable.text = model.commodity_name;
    
    NSArray *array = [model.selected_options JSONValue];
    NSMutableString *sting =nil;
    for (NSInteger i = 0; i<array.count; i++) {
        NSDictionary *dic = array[i];
        NSString *name = dic[@"name"];
        NSString *valuee= dic[@"value"];
        if (i==0) {
            sting = [NSMutableString stringWithFormat:@"%@:%@",name,valuee];
        }else{
            [sting appendFormat:@"    %@:%@",name,valuee];
        }
        self.productTypeLable.text = sting;
    }
    self.priceLable.text = [NSString stringWithFormat:@"¥%@",model.univalent];
    
    
    //
    self.numberLable.text = [NSString stringWithFormat:@"共:%@件商品",model.count];
    //
    self.moneyLable.text = [NSString stringWithFormat:@"合计:%.2f",[model.univalent doubleValue]* [model.count
                                                                                               doubleValue]];
    
    NSString *sta = [NSString stringWithFormat:@"%@",model.state];
    NSString *stap = [NSString stringWithFormat:@"%@",model.state_step];
    
    if ([sta isEqualToString:@"11"]) {
        if ([stap isEqualToString:@"4"]) {
            [self lableHiden:NO  with:@"交易失败"];
            [self settitle:@"查看物流" Withcolor:DRINK_RED_COLOR WithTwoHiden:YES Withbtn:self.twoButten];
            
            [self settitle:@"查看原因" Withcolor:DRINK_RED_COLOR WithTwoHiden:NO Withbtn:self.oneButten ];
        }else
        {
            [self lableHiden:YES with:nil];
            [self settitle:@"查看物流" Withcolor:DRINK_RED_COLOR WithTwoHiden:YES Withbtn:self.twoButten];
            [self settitle:@"待发货" Withcolor:DRINK_RED_COLOR WithTwoHiden:NO Withbtn:self.oneButten ];
        }
    }else if ([sta isEqualToString:@"13"]) {
        [self lableHiden:YES with:nil];
        [self settitle:@"确认收货" Withcolor:DRINK_RED_COLOR WithTwoHiden:NO Withbtn:self.oneButten];
        [self settitle:@"查看物流" Withcolor:rgb(235, 112, 52, 1) WithTwoHiden:NO Withbtn:self.twoButten];
        
    }else if ([sta isEqualToString:@"14"])
    {
        [self lableHiden:YES with:nil];
        [self settitle:@"申请退货" Withcolor:DRINK_RED_COLOR WithTwoHiden:NO Withbtn:self.oneButten];
        [self settitle:@"删除订单" Withcolor:DRINK_RED_COLOR WithTwoHiden:NO Withbtn:self.twoButten];
        
    }else if ([sta isEqualToString:@"15"])
    {
        switch ([stap intValue]) {
            case 1:
                [self btnhiden:YES];
                [self lableHiden:NO with:@"审核中"];
                break;
            case 2:
                [self btnhiden:YES];
                [self lableHiden:NO with:@"等待总部确认收货"];
                break;
            case 3:
                [self lableHiden:NO with:@"驳回"];
                [self settitle:@"查看原因" Withcolor:DRINK_RED_COLOR WithTwoHiden:NO Withbtn:self.oneButten];
                
                [self settitle:@"删除订单" Withcolor:DRINK_RED_COLOR WithTwoHiden:YES Withbtn:self.twoButten];
                
                break;
            case 4:
                [self btnhiden:YES];
                [self lableHiden:NO with:@"退货成功"];
                
            default:
                break;
        }
    }
    else if ([sta isEqualToString:@"12"])
    {
        [self btnhiden:YES];
        [self lableHiden:NO with:@"订单已取消"];
    }
}
-(void)settitle:(NSString *)title Withcolor:(UIColor *)color WithTwoHiden:(BOOL)Hiden Withbtn:(UIButton *)butten
{
    [butten setTitle:title forState:UIControlStateNormal];
    [butten setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    butten.backgroundColor = color;
    butten.hidden = Hiden;
    //    butten.enabled = enable;
}
-(void)btnhiden:(BOOL)hiden
{
    self.oneButten.hidden = hiden;
    self.twoButten.hidden = hiden;
}
-(void)lableHiden:(BOOL)hiden with:(NSString *)message
{
    self.reasonLable.hidden = hiden;
    
    self.reasonLable.text = message;
    
    self.reasonLable.textColor = DRINK_RED_COLOR;
}

- (IBAction)oneButtenClic:(UIButton*)sender {
    if ([sender.titleLabel.text isEqualToString:@"取消订单"]) {
        
        if ([self.deleget respondsToSelector:@selector(cancelOrder:)]) {
            [self.deleget cancelOrder:self.model];
        }
        
    }else if ([sender.titleLabel.text isEqualToString:@"确认收货"])
    {
        if ([self.deleget respondsToSelector:@selector(masureGetProduct:)]) {
            [self.deleget masureGetProduct:self.model];
        }
        
    }else if ([sender.titleLabel.text isEqualToString:@"申请退货"])
    {
        if ([self.deleget respondsToSelector:@selector(applyBackProduct:)]) {
            [self.deleget applyBackProduct:self.model];
        }
        
    }else if ([sender.titleLabel.text isEqualToString:@"查看原因"])
    {
        if ([self.deleget respondsToSelector:@selector(cleckReason:)]) {
            [self.deleget cleckReason:self.model ];
        }
        
    }

    
    
}
- (IBAction)twoButtenClic:(UIButton *)sender {
    if ([sender.titleLabel.text isEqualToString:@"查看物流"]) {
        if ([self.deleget respondsToSelector:@selector(loocForWuliu:)]) {
            [self.deleget loocForWuliu:self.model];
        }
    }else if ([sender.titleLabel.text isEqualToString:@"删除订单"])
    {
        if ([self.deleget respondsToSelector:@selector(delegetOrder:)]) {
            [self.deleget delegetOrder:self.model];
        }
        
    }

    
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
